package com.share.lottery.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.share.lottery.R;
import com.share.lottery.model.LotteryBean;

/**
 * @author py
 *选择彩票的子类
 */
public class CpSelectSubAdapter extends BaseAdapter {
	private int selectedPosition = -1;     
	private Context context;
	private LayoutInflater layoutInflater;
	private List<LotteryBean> secTions = new ArrayList<LotteryBean>();
	public int regpoition;
	private ListView subtListView;

	public CpSelectSubAdapter(Context context) {
		this.context = context;
		
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	@Override
	public int getCount() {
	
		return secTions.size();
	}

	@Override
	public LotteryBean getItem(int position) {
		
		return secTions.get(position);
	}

	@Override
	public long getItemId(int position) {
	
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	
		ViewHolder viewHolder = null;
		
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.sublist_item, null);
			
			viewHolder = new ViewHolder();
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.textview1);
			viewHolder.layout=(LinearLayout)convertView.findViewById(R.id.colorlayout);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(selectedPosition == position)   
		{   
			viewHolder.textView.setTextColor(Color.BLUE);   
			viewHolder.layout.setBackgroundResource(R.color.gray);
		} else {   
			viewHolder.textView.setTextColor(Color.WHITE);   
			viewHolder.layout.setBackgroundColor(Color.TRANSPARENT);   
		}   
		viewHolder.textView.setText(secTions.get(position).getName());
		viewHolder.textView.setTextColor(Color.BLACK);
		
		return convertView;
	}
	
	public void addData(List<LotteryBean> sections){
		this.secTions.addAll(sections);
		
//		this.subtListView.destroyDrawingCache();
		notifyDataSetChanged();
//		notifyDataSetInvalidated();
		Log.i("test", "subAdaper数据="+sections.size());
	}
	
	public void clearData(){
		this.secTions.clear();
		
//		this.subtListView.destroyDrawingCache();
		notifyDataSetChanged();
//		notifyDataSetInvalidated();
		
	}
	public static class ViewHolder {
		public TextView textView;
		public LinearLayout layout;
	}
	public void setSelectedPosition(int position) {   
		selectedPosition = position;   
		notifyDataSetChanged();
//		notifyDataSetInvalidated();
	}

	public void setListView(ListView subtListView) {
		// TODO Auto-generated method stub
		this.subtListView=subtListView;
	}   

}
